﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Kugar.Core.Data.MSSQLHelper;
using Kugar.Core.ExtMethod;

namespace Kugar.Core.Security.Providers.MSSQL
{
    public  class MSSQL_UserSpecialManager: IUserSpecialManager
    {
        private Func<SQLDataHelper> _connGetter;
        private bool _isAutoDisposeConn = false;

        public MSSQL_UserSpecialManager( Func<SQLDataHelper> connGetter, bool isAutoDisposeConn = false)
        {
            _connGetter = connGetter;
            _isAutoDisposeConn = isAutoDisposeConn;
        }

        public bool UserSpecial_SetPermissionByID(int userID, int permissionID, int controlValue)
        {
            var conn = GetConnection();

            try
            {
                return conn.ExecuteProcedureNoQuery("p_UserPermission_SetUserPermissionByID", false, new SqlParameter[]
                    {
                        new SqlParameter("@UserID", userID),
                        new SqlParameter("@PermissionID", permissionID),
                        new SqlParameter("@ControlValue", controlValue),
                    }) > 0;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (_isAutoDisposeConn)
                {
                    conn.Dispose();
                }
            }
                
            
        }

        public RMS_PermissionValueCollection UserSpecial_GetByID(int userID)
        {
            var conn = GetConnection();

            try
            {
                conn.ExecuteProcedureNoQuery("[p_UserPermission_PermissionClear]", false,
                                             new[] {new SqlParameter("@UserID", userID),});

                var cmd = conn.CreateCommand();
                cmd.CommandText = "p_UserPermission_PermissionClear";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddRange(conn.GetProcedureParameters(cmd.CommandText));
                return null;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                if (_isAutoDisposeConn)
                {
                    conn.Dispose();
                }
            }
        }

        public bool UserSpecial_UpdatePermission(int userID, RMS_PermissionItemValue[] permissionList)
        {
            var conn = GetConnection();
            
                bool isSuccess = true;


                conn.BeginTransaction();

                conn.ExecuteProcedureNoQuery("p_UserPermission_PermissionClear", false, new[] { new SqlParameter("@UserID", userID), });

                var cmd = conn.CreateCommand();
                cmd.CommandText = "p_UserPermission_InsertPermission";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddRange(conn.GetProcedureParameters(cmd.CommandText));

                for (int i = 0; i < permissionList.Length; i++)
                {
                    cmd.Parameters.SetValue("@UserID", userID);
                    cmd.Parameters.SetValue("@PermissionID", permissionList[i].PermissionID);
                    cmd.Parameters.SetValue("@ControlValue", permissionList[i].ControlValue);

                    try
                    {
                        conn.ExecuteNoQuery(cmd);
                    }
                    catch (Exception)
                    {
                        isSuccess = false;
                        conn.RollbackTransaction();
                        break;
                    }
                }

                if (isSuccess)
                {
                    conn.CommitTransaction();
                }

                if (_isAutoDisposeConn)
                {
                    conn.Dispose();
                }

                return isSuccess;


            
        }

        public bool UserSpecial_Delete(int userID)
        {
            var conn = GetConnection();

            try
            {
                return conn.ExecuteProcedureNoQuery("p_UserPermission_PermissionClear", false,
                                                    new[] {new SqlParameter("@UserID", userID),}) > 0;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                if (_isAutoDisposeConn)
                {
                    conn.Dispose();
                }
            }
            
        }

        private SQLDataHelper GetConnection()
        {
            return _connGetter();
        }

        #region Implementation of IDisposable

        public void Dispose()
        {
            //_pool = null;
        }

        #endregion
    }
}
